<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests\InsertMainRequest;
use App\Http\Requests\IndexRequest;
use App\Http\Requests\UpdateMainRequest;
use App\Http\Requests\UpdateStyleRequest;
use App\Http\Requests\InsertStyleRequest;
use App\Http\Requests\InsertParamRequest;
use App\Http\Requests\InsertPicRequest;
use App\Http\Requests\comment\CommentRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    /**
	 * 加载商品基本信息添加页面
	 * /admin/goods/add
     */
    public function getAdd(Request $request) {

        // 获取底层列表
    	$data = self::getList();

        // 获取用户选择的类别ID
        $tid = $request -> input('tid2',10);

        // 加载视图
    	return view('d.goods.add',['list' => $data,'tid' => $tid]);
    }

    /**
     * 获取底层列表分类 
     * 
     */
    public static function getList() {

        // 设置数组用于存放分类名称
        $data = [];

        // 查询分类并存入数组中
        $res = DB::table('gs_type') -> get();
        foreach($res as $v) {
            if(substr_count($v['path'],',') == '2') {
                $data[] = $v;
            }
        }
        return $data;
    }

    /**
	 * 执行商品基本信息添加操作
	 * /admin/goods/insert
     */
    public function postInsert(InsertMainRequest $request) {
      
        $data = $request -> only(['tid','name','price','on_sale','desc','pic']);
        
    	// 获取上传的图片信息
        $pic = $_FILES['pic'];
        $pic1 = $_FILES['pic1'];
        $pic2 = $_FILES['pic2'];
        $pic3 = $_FILES['pic3'];
    	$pic4 = $_FILES['pic4'];

    	// 执行商品主图片上传
    	$res = doupload($pic,'goods/pic/');

    	// 判断主图片上传是否成功
    	if(!$res['error']) {
    		$error = implode('---',$res['info']);
    		return back() -> with('error',$error);
    	}

        // 执行图片缩放
        imgSmall('goods/pic/'.$res['info'],100,80,'m_');

        // 组合表单信息
        $picname = $res['info'];   

        $data['pic'] = 'm_'.$picname;
        $data['ctime'] = time();

        // 进行列表图片上传
        $i = 1;
        $picname = 'pic1';
        $pic = $$picname;

        while($pic['name']) {
            // 执行商品列表图片上传 
            $res = doupload($pic,'goods/pic/');

            // 判断列表图片上传是否成功
            if(!$res['error']) {
                $error = implode('---',$res['error']);
                return back() -> with('error',$res['error']);
            }
            $data[$picname] = $res['info'];
            if($i==4) {
                break;
            }
            $i++;
            $picname = 'pic'.$i;
            $pic = $$picname;
        }

    	
    	// 商品录入
    	$res = DB::table('goods') -> insertGetId($data);

    	// 判断录入结果
    	if($res) {
            return redirect('admin/goods/style') -> with('gid',$res) -> with('success','商品基本信息添加成功');
    	} else {
    		return back() -> with('error','商品上架失败');
    	}
    }

    /**
     * 商品类别选择页面
     * /admin/goods/choice
     */
    public function getChoice($action) {

        // 获取顶级分类
		$res = DB::table('gs_type') -> where('pid','=','0') -> get();

    	return view('d.goods.choice',['list' => $res,'action' => $action]);
    }

    /**
     * 执行商品类别查询ajax访问
     * /admin/goods/type
     */
    public static function postType(Request $request) {

        // 获取板块PID
    	$pid = $request -> input('pid');

        // 执行查询
    	$res = DB::table('gs_type') -> where('pid','=',$pid) -> get();
    	$res = json_encode($res);
    	echo $res;
    }

    /**
     * 商品浏览页面
     * /admin/goods/index
     */
    public function getIndex(Request $request) {

    	// 获取分页条目
        $page = $request -> input('num',5);
    	
        // 获取商品类别
        $tid = $request -> input('tid2',10);

        // 获取搜索关键词
        $keywords = $request -> input('keywords','');

        // 执行查询
    	$res = DB::table('goods')
    			 -> where('tid','=',$tid)
	    		 -> where('name','like','%'.$keywords.'%')
                 -> orderBy('ctime','desc')
    			 -> paginate($page);
    	if($res) {
    		return view('d.goods.index',['list' => $res,'tid' => $tid,'page' => $page,'keywords' => $keywords]);
    	} else {
    		return back() -> with('error','浏览失败');
    	}
    }

    /**
     * 商品基本信息编辑页面
     * /admin/goods/editmain
     */
    public function getEditmain($id) {
        $res = DB::table('goods') -> where('id','=',$id) -> first();

        return view('d.goods.editmain',['v' => $res,'list' => self::getList()]);
    }

    /**
     * 执行商品基本信息编辑操作
     * /admin/goods/updatemain
     */

    public function postUpdatemain(UpdateMainRequest $request) {

        // 提取表单信息
        $data = $request -> only('id','name','price','on_sale','desc');

        // 执行主图上传更改
        if($_FILES['pic']['name']) {
            $res = doupload($_FILES['pic'],'goods/pic/');
            if($res['error']) {
                $oldpic = DB::table('goods') -> select('pic') -> where('id','=',$request -> input('id')) -> first()['pic'];
                if(file_exists('goods/pic/'.$oldpic)) {
                    unlink('goods/pic/'.$oldpic);
                };           
                $data['pic'] = $res['info'];
            } else {
                return back() -> with('error','图片上传失败');
            }
        }

        // 提取图片列表信息
        $pic1 = $_FILES['pic1'];
        $pic2 = $_FILES['pic2'];
        $pic3 = $_FILES['pic3'];
        $pic4 = $_FILES['pic4'];
        
        // 进行列表图片上传
        for($i = 1;$i < 5;$i++) {
            $picname = 'pic'.$i;
            $pic = $$picname;
            if($pic['name']) {

                // 执行商品列表图片上传 
                $res = doupload($pic,'goods/pic/');

                // 判断列表图片上传是否成功
                if(!$res['error']) {
                    $error = implode('---',$res['error']);
                    return back() -> with('error',$error);
                }
                $data[$picname] = $res['info'];

                // 查询是否有原图片
                $oldpic = DB::table('goods') -> select($picname) -> where('id','=',$request -> input('id')) -> first()[$picname];
                if($oldpic && file_exists('goods/pic/'.$oldpic)) {
                    unlink('goods/pic/'.$oldpic);
                }; 

            }
        }
       
        // 执行数据编辑操作
        $res = DB::table('goods') -> where('id','=',$data['id']) -> update($data);

        // 判断编辑是否成功
        if($res) {
            return redirect('/admin/goods/index?tid2='.$request -> input('tid')) -> with('success','编辑基本信息成功');
        } else {
            return back() -> with('error','编辑基本信息失败');
        }
    }

    /**
     * 执行商品的删除操作
     * /admin/goods/goodsdelete
     */ 
    public function getDelete($id) {

        // 创建一个数组用来存储商品相关的图片
        $arr = [];

        // 取出商品主表中的图片
        $goods = DB::table('goods') -> where('id',$id) -> first();
        foreach($goods as $k => $v) {
            if(substr_count($k,'pic') > 0) {
                if($v) {
                    $arr[] = $v;
                }
            }
        }

        // 取出商品款式表中的图片
        $gs_style = DB::table('gs_style') -> where('gid',$id) -> get();
        foreach($gs_style as $v) {
            if($v) {
                $arr[] = $v['pic'];
            }
        }

        // 取出商品的穿着效果图
        $show_wear = DB::table('show_wear') -> where('gid',$id) -> get();
        foreach($show_wear as $v) {
            if($v) {
                $arr[] = $v['pic'];
            };
        }

        // 取出商品的款式效果图
        $show_style = DB::table('show_style') -> where('gid',$id) -> get();
        foreach($show_style as $v) {
            if($v) {
                $arr[] = $v['pic'];
            }
        }

        // 取出商品的细节做工图
        $show_detail = DB::table('show_detail') -> where('gid',$id) -> get();
        foreach($show_detail as $v) {
            if($v) {
                $arr[] = $v['pic'];
            }
        }

        // 删除该商品的相关数据
        $res1 = DB::table('show_detail') -> where('gid',$id) -> delete();
        $res2 = DB::table('show_style') -> where('gid',$id) -> delete();
        $res3 = DB::table('show_wear') -> where('gid',$id) -> delete();
        $res4 = DB::table('gs_param') -> where('gid',$id) -> delete();
        $res5 = DB::table('gs_style') -> where('gid',$id) -> delete();
        $res6 = DB::table('gs_size') -> where('gid',$id) -> delete();
        $res7 = DB::table('goods') -> where('id',$id) -> delete();

        if($res7) {

            // 删除和商品有关的图片资源
            foreach($arr as $v) {
                if(file_exists('goods/pic'.$v)) {
                    unlink('goods/pic'.$v);
                }
            }
            return back() -> with('success','删除成功！');
        } else {
            return back() -> with('error','删除失败!');
        }

    }

    /**
     * 加载商品评论列表
     * /admin/goods/commentlist
     */ 
    public function getCommentlist($gid) {
        $res = DB::select('select gs_comment.*,user.username from gs_comment,user where user.id=gs_comment.uid and gs_comment.gid='.$gid);
        foreach($res as &$v) {
            $v['score'] = floor($v['pri_score'] + $v['qua_score'] + $v['des_score']) / 3;
            $v['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
        }
        return view('d.goods.commentlist',['list' => $res,'gid' => $gid]);
    }

    /**
     * 加载编辑评论表
     * /admin/goods/editcomment
     */ 
    public function getEditcomment($gid,$id) {
        $comment_list = DB::select('select gs_comment.*,user.username,user_trade.style_id,size_id,gs_style.desc,gs_size.size from gs_comment,user,user_trade,gs_style,gs_size where gs_comment.uid=user.id and gs_comment.traid=user_trade.id and user_trade.style_id=gs_style.id and user_trade.size_id=gs_size.id and gs_comment.id='.$id);
        preg_match('/<img.*?>/',$comment_list[0]['contents'],$pic);
        $comment_list[0]['pic'] = $pic;
        foreach($pic as $v) {
            $comment_list[0]['contents'] = str_replace($v,'',$comment_list[0]['contents']);
        }
        $reply_list = DB::select('select gs_reply.*,user.username from gs_reply,user where gs_reply.comid='.$id.' and gs_reply.uid=user.id');
        foreach($reply_list as &$v) {
            preg_match('/<img.*?>/',$v['reply'],$pic);
            if($pic) {
                $v['pic'] = $pic;
                $v['reply'] = str_replace($v['pic'],'',$v['reply']);
            }
            
        }
        return view('d.goods.editcomment',['comment_list' => $comment_list[0],'reply_list' => $reply_list]);
    }

    /**
     * 执行评论添加操作
     * /admin/goods/reply
     */ 
    public function postReply(CommentRequest $request) {

        // 取出数据库所需要的元素存入数组中
        $data['reply'] = $request -> input('editorValue');
        $data['uid'] = session('adminuser')['id'];
        $data['ctime'] = time();
        $data['comid'] = $request -> input('comid');

        // 执行数据库插入操作
        $res = DB::table('gs_reply') -> insert($data);
        if($res) {
            return back() -> with('success','回复成功！');
        } else {
            return back() -> with('error','回复失败！');
        }
    }

    /**
     * 商品详细信息编辑选择页面
     * /admin/goods/choicedetail
     */ 
    public function getEditdetail($id) {

        return view('d.goods.editdetail',['gid' => $id]);
    }

    /**
     * 商品款式列表
     * /admin/goods/stylelist
     */ 
    public function getStylelist($gid,Request $request) {
        $keywords = $request -> input('keywords','');
        $res = DB::table('gs_style')
                 -> where('gid',$gid) 
                 -> get();

        foreach($res as &$v) {
            $size = '';
            $res1 = DB::table('gs_size') -> where('sid',$v['id']) -> select('size') -> get();
            foreach($res1 as $value) {
                $size .= $value['size'].',';
            }
            $size = rtrim($size,',');
            $v['size'] = $size;            
        }
        return view('d.goods.stylelist',['gid' => $gid,'list' => $res]);
    }

    /**
     * 执行删除商品款式操作
     * /admin/goods/deletestyle
     */ 
    public function getDeletestyle($sid) {

        // 查找图片
        $pic = DB::table('gs_style') -> where('id',$sid) -> select('pic') -> first()['pic'];

        // 执行删除操作
        $res = DB::table('gs_style') -> where('id',$sid) -> delete();
        $res1 = DB::table('gs_size') -> where('sid',$sid) -> delete();

        // 判断删除是否成功
        if($res) {

            // 删除图片
            if(file_exists('goods/pic/'.$pic)) {
                unlink('goods/pic/'.$pic);
            } 
            return back() -> with('success','删除款式成功');
        } else {
            return back() -> with('error','删除款式失败');
        }
    }

    /**
     * 加载商品尺码列表
     * /admin/goods/sizelist
     */ 
    public function getSizelist($gid,$sid,Request $request) {

        $keywords = $request -> input('keywords','');

        $res = DB::table('gs_size') -> where('sid',$sid) -> get();
        return view('d.goods.sizelist',['list' => $res,'gid' => $gid,'sid' => $sid,'keywords' => $keywords]);
    }

    /**
     * 执行删除商品尺寸操作
     * /admin/goods/deletesize
     */ 
    public function getDeletesize($id) {

        // 执行删除操作
        $res = DB::table('gs_size') -> where('id',$id) -> delete();

        // 判断删除是否成功
        if($res) {
            return back() -> with('success','删除尺码成功');
        } else {
            return back() -> with('error','删除尺码失败');
        }
    }

    /**
     * 加载商品参数列表
     * /admin/goods/paramlist
     */ 
    public function getParamlist($gid) {
        $res = DB::table('gs_param') -> where('gid',$gid) -> get();
        // dd($res);
        return view('d.goods.paramlist',['list' => $res,'gid' => $gid]);
    }

    /**
     * 执行删除商品参数操作
     * /admin/goods/deleteparam
     */ 
    public function getDeleteparam($id) {

        // 执行删除操作
        $res = DB::table('gs_param') -> where('id',$id) -> delete();

        // 判断删除是否成功
        if($res) {
            return back() -> with('success','删除尺码成功');
        } else {
            return back() -> with('error','删除尺码失败');
        }
    }

    /**
     * 加载商品穿着效果图列表
     * /admin/goods/showwearlist
     */ 
    public function getShowwearlist($gid) {
        $res = DB::table('show_wear') -> where('gid',$gid) -> get();
        // dd($res);
        return view('d.goods.showwearlist',['list' => $res,'gid' => $gid]);
    }

     /**
     * 执行删除商品穿着效果图操作
     * /admin/goods/deleteshowwear
     */ 
    public function getDeleteshowwear($id) {

        // 查找图片
        $pic = DB::table('show_wear') -> where('id',$id) -> select('pic') -> first()['pic'];

        // 执行删除操作
        $res = DB::table('show_wear') -> where('id',$id) -> delete();

        // 判断删除是否成功
        if($res) {

            // 删除图片
            if(file_exists('goods/pic/'.$pic)) {
                unlink('goods/pic/'.$pic);
            } 
            return back() -> with('success','删除图片成功');
        } else {
            return back() -> with('error','删除图片失败');
        }
    }

    /**
     * 加载商品款式效果图列表
     * /admin/goods/showstylelist
     */ 
    public function getShowstylelist($gid) {

        // 查询效果图
        $res = DB::table('show_style') -> where('gid',$gid) -> get();
        
        // 加载视图
        return view('d.goods.showstylelist',['list' => $res,'gid' => $gid]);
    }

     /**
     * 执行删除商品款式效果图操作
     * /admin/goods/deleteshowstyle
     */ 
    public function getDeleteshowstyle($id) {

        // 查找图片
        $pic = DB::table('show_style') -> where('id',$id) -> select('pic') -> first()['pic'];

        // 执行删除操作
        $res = DB::table('show_style') -> where('id',$id) -> delete();

        // 判断删除是否成功
        if($res) {

            // 删除图片
            if(file_exists('goods/pic/'.$pic)) {
                unlink('goods/pic/'.$pic);
            } 
            return back() -> with('success','删除图片成功');
        } else {
            return back() -> with('error','删除图片失败');
        }
    }

    /**
     * 加载商品细节效果图列表
     * /admin/goods/showdetaillist
     */ 
    public function getShowdetaillist($gid) {

        // 查询细节效果图
        $res = DB::table('show_detail') -> where('gid',$gid) -> get();
        
        // 加载视图
        return view('d.goods.showdetaillist',['list' => $res,'gid' => $gid]);
    }

     /**
     * 执行删除商品细节效果图操作
     * /admin/goods/deleteshowstyle
     */ 
    public function getDeleteshowdetail($id) {

        // 查找图片
        $pic = DB::table('show_detail') -> where('id',$id) -> select('pic') -> first()['pic'];

        // 执行删除操作
        $res = DB::table('show_detail') -> where('id',$id) -> delete();

        // 判断删除是否成功
        if($res) {

            // 删除图片
            if(file_exists('goods/pic/'.$pic)) {
                unlink('goods/pic/'.$pic);
            } 
            return back() -> with('success','删除图片成功');
        } else {
            return back() -> with('error','删除图片失败');
        }
    }

    /**
     * 商品款式选择页面
     * /admin/goods/choicestyle
     */ 
    public function getChoicestyle($gid) {

        // 查询商品已有款式
        $res = DB::table('gs_style') -> where('gid','=',$gid) -> get();

        // 加载视图
        return view('d.goods.choicestyle',['list' => $res,'gid' => $gid]) -> with('gid',$gid);
    }

    /**
     * 加载商品款式编辑页面 
     * /admin/goods/editstyle
     */ 
    public function getEditstyle($gid,$sid) {

        // 查询款式列表
        $stylelist = DB::table('gs_style') -> where('gid',$gid) -> first();
        
        // 查询尺寸列表
        $sizelist = DB::table('gs_size') -> where('sid',$sid) -> get();
        
        // 加载视图
        return view('d.goods.editstyle',['stylelist' => $stylelist,'sizelist' => $sizelist]);
    }

    /**
     * 执行商品款式编辑操作 
     * /admin/goods/updatestyle
     */ 
    public function postUpdatestyle(UpdateStyleRequest $request) {

    // 执行款式更新
        // 获取款式表所需内容
        $data = $request -> only(['gid','desc']);

        // 检验是否有图片上传
        if($_FILES['pic']['name']){
            $res = doupload($_FILES['pic'],'goods/pic/');
            if(!$res['error']) {
                $error = implode('----',$res['error']);
                return back() -> with('error',$error);
            }
            
            // 删除原有图片
            $oldpic = DB::table('gs_style') -> where('id',$request -> input('sid')) -> select('pic') -> first()['pic'];
            if($oldpic && file_exists('goods/pic/'.$oldpic)) {
                // dd($oldpic);
                unlink('goods/pic/'.$oldpic);
            };
            $data['pic'] = $res['info'];
        }
        
        // 执行款式表修改
        $res1 = DB::table('gs_style') -> where('id',$request -> input('sid')) -> update($data);

    // 执行尺码更新
        // 开始事务回滚
        DB::beginTransaction();

        // 删除旧数据
        $res = DB::table('gs_size') -> where('sid',$request -> input('sid')) ->delete();

        // 插入新数据
        foreach($request -> all() as $k => $v) {
            static $i = 1;

            // 获取用户的尺寸信息
            if(substr_count($k,'size')) {
                $data = [];
                $data['size'] = $v;
                $data['gid'] = $request -> input('gid');
                $data['sid'] = $request -> input('sid');
                $data['store_num'] = $request -> input('store_num'.$i);
                
                $res = DB::table('gs_size') -> insert($data);
                if(!$res) {
                    DB::rollback();
                    return back() -> with('error','编辑款式信息失败') -> with('gid',$data['gid']);
                }
                $i++;
            }
        }
            DB::commit();
            return redirect('admin/goods/stylelist/'.$data['gid']) -> with('success','编辑款式信息成功');
    }

    /**
     * 加载修改商品参数页面
     * /admin/goods/editparam
     */ 
    public function getEditparam($gid) {

        // 查询参数列表
        $res = DB::table('gs_param') -> where('gid',$gid) -> get();

        // 加载视图
        return view('d.goods.editparam',['list' => $res,'gid' => $gid]);
    }

    /**
     * 执行商品参数修改操作
     * /admin/goods/updateparam
     */ 
    public function postUpdateparam(Request $request) {

        // 开始事务回滚
        DB::beginTransaction();

        // 删除旧数据
        $res = DB::table('gs_param') -> where('gid',$request -> input('gid')) ->delete();

        // 插入新数据
        foreach($request -> all() as $k => $v) {
            static $i = 1;

            // 获取用户的参数信息
            if(substr_count($k,'name')) {
                $data = [];
                $data['name'] = $v;
                $data['gid'] = $request -> input('gid');
                $data['content'] = $request -> input('content'.$i);
                $res = DB::table('gs_param') -> insert($data);
                if(!$res) {
                    DB::rollback();
                    return back() -> with('error','编辑商品款式失败') -> with('gid',$data['gid']);
                }
                $i++;
            }
        }
        DB::commit();
        return redirect('/admin/goods/paramlist/'.$data['gid']) -> with('success','编辑商品款式成功');
    }

    /**
     * 加载修改商品穿着效果图页面
     * /admin/goods/editshowwear
     */ 
    public function getEditshowwear($gid) {

        // 查询穿着效果图
        $res = DB::table('show_wear') -> where('gid',$gid) -> get();

        // 加载视图
        return view('d.goods.editshowwear',['list' => $res,'gid' => $gid]);
    }

    /**
     * 执行商品穿着效果图修改操作
     * /admin/goods/updateshowwear
     */ 
    public function postUpdateshowwear(Request $request) {

        // 查询数据库中该商品原有的记录数量
        $gid = $request -> input('gid');
        $count = DB::table('show_wear') -> where('gid',$request -> input('gid')) -> count();

        // 循环操作数据
        for($i = 1;$i <6;$i++) {
            
            // 对数据库中已有的数据进行更新
            if($i <= $count) { 

                // 取出数据库中的该条数据
                $olddata = DB::table('show_wear') -> where('gid',$request -> input('gid')) -> get()[$i - 1];
                $oldpic = $olddata['pic']; 

                // 判断是否上传图片
                if($_FILES['pic'.$i]['name']) {

                    // 执行图片上传
                    $res = doupload($_FILES['pic'.$i],'goods/pic/');

                    // 判断是否上传成功
                    if(!$res['error']) {
                        return back() -> with('error','图片上传失败') -> with('gid',$request -> input('gid'));
                    }
                    $data['pic'] = $res['info'];

                    // 删除原有图片
                    unlink('goods/pic/'.$oldpic);
                } else {

                    // 用户没有上传图片
                    $data['pic'] = $oldpic;
                }    

                // 进行数据库数据修改
                $data['order'] = $request -> input('order'.$i);
                $res = DB::table('show_wear') -> where('id',$olddata['id']) -> update($data);
            } else {

                // 判断用户是否有新增图片
                if(isset($_FILES['pic'.$i]) && $_FILES['pic'.$i]['name']) {

                    // 指定图片上传
                    $res = doupload($_FILES['pic'.$i],'goods/pic/');

                    // 判断是否上传成功
                    if(!$res['error']) {
                        return back() -> with('error','图片上传失败') -> with('gid',$request -> input('gid'));
                    }

                    // 将数据存入data数组并插入数据库
                    $data['pic'] = $res['info'];
                    $data['order'] = $request -> input('order'.$i);
                    $gid = $request -> input('gid');
                    $data['gid'] = $gid;
                    $res = DB::table('show_wear') -> insert($data);
                } else {
                    break;
                }
            }
        }
        return redirect('/admin/goods/showwearlist/'.$gid) -> with('success','编辑穿着效果图成功');
        
    }

    /**
     * 加载修改商品款式效果图页面
     * /admin/goods/editshowstyle
     */ 
    public function getEditshowstyle($gid) {

        // 查询款式效果图
        $res = DB::table('show_style') -> where('gid',$gid) -> get();

        // 加载视图
        return view('d.goods.editshowstyle',['list' => $res,'gid' => $gid]);
    }

    /**
     * 执行商品款式效果图修改操作
     * /admin/goods/updateshowstyle
     */ 
    public function postUpdateshowstyle(Request $request) {

        // 查询数据库中该商品原有的记录数量
        $count = DB::table('show_style') -> where('gid',$request -> input('gid')) -> count();

        // 循环操作数据
        for($i = 1;$i <6;$i++) {
            
            // 对数据库中已有的数据进行更新
            if($i <= $count) { 

                // 取出数据库中的该条数据
                $olddata = DB::table('show_style') -> where('gid',$request -> input('gid')) -> get()[$i - 1];
                $oldpic = $olddata['pic']; 

                // 判断是否上传图片
                if($_FILES['pic'.$i]['name']) {

                    // 执行图片上传
                    $res = doupload($_FILES['pic'.$i],'goods/pic/');

                    // 判断是否上传成功
                    if(!$res['error']) {
                        return back() -> with('error','图片上传失败') -> with('gid',$request -> input('gid'));
                    }
                    $data['pic'] = $res['info'];

                    // 删除原有图片
                    unlink('goods/pic/'.$oldpic);
                } else {

                    // 用户没有上传图片
                    $data['pic'] = $oldpic;
                }    

                // 进行数据库数据修改
                $data['order'] = $request -> input('order'.$i);
                $res = DB::table('show_style') -> where('id',$olddata['id']) -> update($data);
            } else {

                // 判断用户是否有新增图片
                if(isset($_FILES['pic'.$i]) && $_FILES['pic'.$i]['name']) {

                    // 指定图片上传
                    $res = doupload($_FILES['pic'.$i],'goods/pic/');

                    // 判断是否上传成功
                    if(!$res['error']) {
                        return back() -> with('error','图片上传失败') -> with('gid',$request -> input('gid'));
                    }

                    // 将数据存入data数组并插入数据库
                    $data['pic'] = $res['info'];
                    $data['order'] = $request -> input('order'.$i);
                    $gid = $request -> input('gid');
                    $data['gid'] = $gid;
                    $res = DB::table('show_style') -> insert($data);
                } else {
                    break;
                }
            }
        }
        return redirect('/admin/goods/showstylelist/'.$request -> input("gid")) -> with('success','编辑款式效果图成功');
        
    }

    /**
     * 加载修改商品细节做工图页面
     * /admin/goods/editshowdetail
     */ 
    public function getEditshowdetail($gid) {

        // 查询细节做工图
        $res = DB::table('show_detail') -> where('gid',$gid) -> get();

        // 加载视图
        return view('d.goods.editshowdetail',['list' => $res,'gid' => $gid]);
    }

    /**
     * 执行商品细节做工图修改操作
     * /admin/goods/updateshowdetail
     */ 
    public function postUpdateshowdetail(Request $request) {

        // 查询数据库中该商品原有的记录数量
        $count = DB::table('show_detail') -> where('gid',$request -> input('gid')) -> count();

        // 循环操作数据
        for($i = 1;$i <6;$i++) {
            
            // 对数据库中已有的数据进行更新
            if($i <= $count) { 

                // 取出数据库中的该条数据
                $olddata = DB::table('show_detail') -> where('gid',$request -> input('gid')) -> get()[$i - 1];
                $oldpic = $olddata['pic']; 

                // 判断是否上传图片
                if($_FILES['pic'.$i]['name']) {

                    // 执行图片上传
                    $res = doupload($_FILES['pic'.$i],'goods/pic/');

                    // 判断是否上传成功
                    if(!$res['error']) {
                        return back() -> with('error','图片上传失败') -> with('gid',$request -> input('gid'));
                    }
                    $data['pic'] = $res['info'];

                    // 删除原有图片
                    unlink('goods/pic/'.$oldpic);
                } else {

                    // 用户没有上传图片
                    $data['pic'] = $oldpic;
                }    

                // 进行数据库数据修改
                $data['order'] = $request -> input('order'.$i);
                $res = DB::table('show_detail') -> where('id',$olddata['id']) -> update($data);
            } else {

                // 判断用户是否有新增图片
                if(isset($_FILES['pic'.$i]) && $_FILES['pic'.$i]['name']) {

                    // 指定图片上传
                    $res = doupload($_FILES['pic'.$i],'goods/pic/');

                    // 判断是否上传成功
                    if(!$res['error']) {
                        return back() -> with('error','图片上传失败') -> with('gid',$request -> input('gid'));
                    }

                    // 将数据存入data数组并插入数据库
                    $data['pic'] = $res['info'];
                    $data['order'] = $request -> input('order'.$i);
                    $gid = $request -> input('gid');
                    $data['gid'] = $gid;
                    $res = DB::table('show_detail') -> insert($data);
                } else {
                    break;
                }
            }
        }
        return redirect('/admin/goods/showdetaillist/'.$request -> input('gid')) -> with('success','编辑细节做工图成功');
        
    }

    /**
     * 执行商品款式尺寸添加操作
     * /admin/goods/insertstyle
     */
    public function postInsertstyle(InsertStyleRequest $request) {

        // 获取款式表所需内容
        $data = $request -> only(['gid','desc']);

        // 上传款式图片
        $res = doupload($_FILES['pic'],'goods/pic/');
        if(!$res['error']) {
            $error = implode('----',$res['error']);
            return back() -> with('error',$error);
        }
        $data['pic'] = $res['info'];

        // 执行款式插入操作
        $style_id = DB::table('gs_style') -> insertGetId($data);

        // 判断款式是否插入成功
        if($style_id) {
            $arr['gid'] = $data['gid'];
            $arr['sid'] = $style_id;
            $data = [];

            // 遍历添加尺寸信息
            foreach($request -> all() as $k => $v) {
                static $i = 1;

                // 获取用户的尺寸信息
                if(substr_count($k,'size')) {
                    $data['size'] = $v;
                    $data['gid'] = $arr['gid'];
                    $data['sid'] = $arr['sid'];
                    $data['store_num'] = $request -> input('store_num'.$i);
                    
                    $res = DB::table('gs_size') -> insert($data);
                    if(!$res) {
                        return back() -> with('error','添加款式尺寸失败') -> with('gid',$data['gid']);
                    }
                    $i++;
                }
            }

            if($request -> input('next') == '1') {
                return redirect('admin/goods/param') -> with('gid',$data['gid']) -> with('success','添加款式尺寸成功');
            } else if($request -> input('next') == '0') {
                return redirect('admin/goods/style') -> with('gid',$data['gid']) -> with('success','添加款式尺寸成功');
            } else {
                return redirect('/admin/goods/editdetail/'.$data['gid']) -> with('success','添加款式尺寸成功');
            }
        } else {
            return back() -> with('error','添加款式尺寸失败') -> with('gid',$data['gid']);
        }      
    }

    /**
     * 加载商品款式尺寸页面session
     * /admin/goods/style
     */
    public function getStyle() {
        // 加载视图
        return view('d.goods.style',['gid' => session('gid')]);
    }

    /**
     * 加载商品款式尺寸页面
     * /admin/goods/style2
     */
    public function getStyle2($gid) {

        return view('d.goods.style',['gid' => $gid,'flag' => 'off']);
    }

    /**
     * 加载商品参数添加页面
     * /admin/goods/param
     */
    public function getParam() {

        return view('d.goods.param',['gid' => session('gid')]);
    }

    /**
     * 加载商品参数添加页面
     * /admin/goods/param
     */
    public function postInsertparam(InsertParamRequest $request) {
        
        // 初始化数组存储数据
        $data = [];

        // 设置开关检验操作是否成功
        $flag = true;

        // 取出商品ID
        $data['gid'] = $request -> input('gid');

        // 遍历插入表单数据
        foreach($request -> all() as $k => $v) {
            static $i = 1;
            if(substr_count($k,'name')) {
                $data['name'] = $request -> input('name'.$i);
                $data['content'] = $request -> input('content'.$i);
                $res = DB::table('gs_param') -> insert($data);
                if(!$res) {
                    $flag = false;
                    return;
                }
                $i++;
            }
        }

        if($flag) {
            return redirect('/admin/goods/showwear') -> with('success','添加参数成功') -> with('gid',$data['gid']);
        } else {
            return back() -> with('error','添加参数失败') -> with('gid',$data['gid']);
        }

    }
   
    /**
     * 加载商品穿着效果添加页面
     * /admin/goods/showwear
     */
    public function getShowwear() {

        return view('d.goods.showwear',['gid' => session('gid')]);
    }

    /**
     * 执行商品穿着效果图添加操作
     * /admin/goods/insertshowwear
     */
    public function postInsertshowwear(InsertPicRequest $request) {

        $data = [];
        $data['gid'] = $request -> input('gid');
        $flag = true;
        // 遍历取出款式图片
        foreach($_FILES as $value) {
            if ($value['name']!='') {
                $pic[] = $value;
            }
        }

        // 遍历上传款式图片
        if(isset($pic)) {
            foreach($pic as $v) {

                // 执行图片上传
                $res = doupload($v,'goods/pic');

                // 判断图片是否上传成功
                static $num = 1;
                if($res['error']) {

                // 记录图片名字             
                $data['pic'] = $res['info'];
                $data['order'] = $request -> input('order'.$num);
                $res = DB::table('show_wear') -> insert($data);
                if(!$res) {
                    $flag = false;
                    return;
                }
                $num++;
                } else {
                    return back() -> with('error','图片上传失败');
                }
            } 
        }   
        
        if($flag) {
            return redirect('/admin/goods/showstyle') -> with('success','添加效果图成功') -> with('gid',$data['gid']);
        } else {
            return back() -> with('error','添加效果图失败') -> with('gid',$data['gid']);
        }

    }

    /**
     * 加载商品款式效果添加页面
     * /admin/goods/showwear
     */
    public function getShowstyle() {

        return view('d.goods.showstyle',['gid' => session('gid')]);
    }

    /**
     * 执行商品款式效果图添加操作
     * /admin/goods/insertshowwear
     */
    public function postInsertshowstyle(InsertPicRequest $request) {

        $data = [];
        $data['gid'] = $request -> input('gid');
        $flag = true;
        // 遍历取出款式图片
        foreach($_FILES as $value) {
            if ($value['name']!='') {
                $pic[] = $value;
            }
        }

        // 遍历上传款式图片
        if(isset($pic)) {
            foreach($pic as $v) {

                // 执行图片上传
                $res = doupload($v,'goods/pic');

                // 判断图片是否上传成功
                static $num = 1;
                if($res['error']) {

                // 记录图片名字             
                $data['pic'] = $res['info'];
                $data['order'] = $request -> input('order'.$num);
                $res = DB::table('show_style') -> insert($data);
                if(!$res) {
                    $flag = false;
                    return;
                }
                $num++;
                } else {
                    return back() -> with('error','图片上传失败');
                }
            } 
        }   
        
        if($flag) {
            return redirect('/admin/goods/showdetail') -> with('success','添加效果图成功') -> with('gid',$data['gid']);
        } else {
            return back() -> with('error','添加效果图失败') -> with('gid',$data['gid']);
        }

    }

     /**
     * 加载商品细节效果添加页面
     * /admin/goods/showdetail
     */
    public function getShowdetail() {

        return view('d.goods.showdetail',['gid' => session('gid')]);
    }

    /**
     * 执行商品细节做工图添加操作
     * /admin/goods/insertshowdetail
     */
    public function postInsertshowdetail(InsertPicRequest $request) {

        $data = [];
        $data['gid'] = $request -> input('gid');
        $flag = true;
        // 遍历取出款式图片
        foreach($_FILES as $value) {
            if ($value['name']!='') {
                $pic[] = $value;
            }
        }

        // 遍历上传款式图片
        if(isset($pic)) {
            foreach($pic as $v) {

                // 执行图片上传
                $res = doupload($v,'goods/pic');

                // 判断图片是否上传成功
                static $num = 1;
                if($res['error']) {

                // 记录图片名字             
                $data['pic'] = $res['info'];
                $data['order'] = $request -> input('order'.$num);
                $res = DB::table('show_detail') -> insert($data);
                if(!$res) {
                    $flag = false;
                    return;
                }
                $num++;
                } else {
                    return back() -> with('error','图片上传失败');
                }
            } 
        }   
        
        $tid = DB::table('goods') -> where('id',$request -> input('gid')) -> select('tid') -> first()['tid'];
        if($flag) {
            return redirect('/admin/goods/index?tid2='.$tid) -> with('success','添加效果图成功') -> with('gid',$data['gid']);
        } else {
            return back() -> with('error','添加效果图失败') -> with('gid',$data['gid']);
        }

    }
}
